package presentacion;

import javax.swing.JPanel;
import java.awt.GridBagLayout;
import javax.swing.JScrollPane;
import java.awt.GridBagConstraints;
import javax.swing.JTable;
import java.awt.Insets;
import java.util.ArrayList;
import java.util.Iterator;

import javax.swing.JButton;
import javax.swing.table.DefaultTableModel;
import javax.swing.JCheckBox;
import javax.swing.SwingConstants;
import javax.swing.JTextPane;

import negocio.DirectorEstrategia;

import negocio.TransferDirector;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class DirectorPanelMarketingEstrategias extends JPanel implements DirectorPanel {
	private JTable table;
	private JButton btnAceptar;
	private JButton btnDenegar;
	private JCheckBox chckbxMostrarSoloPendientes;
	private JTextPane textPane;
	
	
	private ArrayList<DirectorEstrategia> listaEstrategias;

	/**
	 * Create the panel.
	 */
	public DirectorPanelMarketingEstrategias() {
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[]{288, 0, 0, 0};
		gridBagLayout.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0};
		gridBagLayout.columnWeights = new double[]{1.0, 1.0, 0.0, Double.MIN_VALUE};
		gridBagLayout.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 1.0, Double.MIN_VALUE};
		setLayout(gridBagLayout);
		
		btnAceptar = new JButton("Aceptar");
		btnAceptar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(table.getSelectedRow()!=-1){
					TransferDirector datos=new TransferDirector();
					datos.setIdEstrategiaResolver(listaEstrategias.get(table.getSelectedRow()).getIdEstrategia());
					datos.setAccionEstrategiaResolver(true);
					SingletonControlador.getInstancia().evento(Evento.DIRECTOR_RESOLVER_ESTRATEGIA, datos);
					actualizar();
				}
			}
		});
		btnAceptar.setEnabled(false);
		GridBagConstraints gbc_btnAceptar = new GridBagConstraints();
		gbc_btnAceptar.fill = GridBagConstraints.HORIZONTAL;
		gbc_btnAceptar.insets = new Insets(0, 0, 5, 5);
		gbc_btnAceptar.gridx = 1;
		gbc_btnAceptar.gridy = 0;
		add(btnAceptar, gbc_btnAceptar);
		
		btnDenegar = new JButton("Denegar");
		btnDenegar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				TransferDirector datos=new TransferDirector();
				datos.setIdEstrategiaResolver(listaEstrategias.get(table.getSelectedRow()).getIdEstrategia());
				datos.setAccionEstrategiaResolver(false);
				SingletonControlador.getInstancia().evento(Evento.DIRECTOR_RESOLVER_ESTRATEGIA, datos);
				actualizar();
			}
		});
		btnDenegar.setEnabled(false);
		GridBagConstraints gbc_btnDenegar = new GridBagConstraints();
		gbc_btnDenegar.fill = GridBagConstraints.HORIZONTAL;
		gbc_btnDenegar.insets = new Insets(0, 0, 5, 5);
		gbc_btnDenegar.gridx = 1;
		gbc_btnDenegar.gridy = 1;
		add(btnDenegar, gbc_btnDenegar);
		
		JScrollPane scrollPane = new JScrollPane();
		GridBagConstraints gbc_scrollPane = new GridBagConstraints();
		gbc_scrollPane.gridheight = 6;
		gbc_scrollPane.insets = new Insets(0, 0, 0, 5);
		gbc_scrollPane.fill = GridBagConstraints.BOTH;
		gbc_scrollPane.gridx = 0;
		gbc_scrollPane.gridy = 0;
		add(scrollPane, gbc_scrollPane);
		
		table = new JTable();
		table.addMouseListener(new MouseAdapter() {
			@Override
			public void mousePressed(MouseEvent arg0) {
				if(table.getSelectedRow()!=-1){
					textPane.setText(listaEstrategias.get(table.getSelectedRow()).getDescripcion());
					if(listaEstrategias.get(table.getSelectedRow()).getFechaResolucion()==null){
						btnAceptar.setEnabled(true);
						btnDenegar.setEnabled(true);
					}else{
						btnAceptar.setEnabled(false);
						btnDenegar.setEnabled(false);
					}
				}
			}
		});
		table.setModel(new DefaultTableModel(
			new Object[][] {
				{null, null, null},
			},
			new String[] {
				"Nombre", "Fecha", "Estado"
			}
		) {
			boolean[] columnEditables = new boolean[] {
				false, false, false
			};
			public boolean isCellEditable(int row, int column) {
				return columnEditables[column];
			}
		});
		table.getColumnModel().getColumn(1).setPreferredWidth(70);
		table.getColumnModel().getColumn(1).setMaxWidth(70);
		table.getColumnModel().getColumn(2).setMaxWidth(75);
		scrollPane.setViewportView(table);
		
		chckbxMostrarSoloPendientes = new JCheckBox("<html><p>Mostrar solo \r\npendientes</p></html>");
		chckbxMostrarSoloPendientes.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				actualizar();
			}
		});
		GridBagConstraints gbc_chckbxMostrarSoloPendientes = new GridBagConstraints();
		gbc_chckbxMostrarSoloPendientes.fill = GridBagConstraints.BOTH;
		gbc_chckbxMostrarSoloPendientes.gridheight = 2;
		gbc_chckbxMostrarSoloPendientes.insets = new Insets(0, 0, 5, 5);
		gbc_chckbxMostrarSoloPendientes.gridx = 1;
		gbc_chckbxMostrarSoloPendientes.gridy = 2;
		add(chckbxMostrarSoloPendientes, gbc_chckbxMostrarSoloPendientes);
		
		textPane = new JTextPane();
		textPane.setEditable(false);
		GridBagConstraints gbc_textPane = new GridBagConstraints();
		gbc_textPane.insets = new Insets(0, 0, 0, 5);
		gbc_textPane.fill = GridBagConstraints.BOTH;
		gbc_textPane.gridx = 1;
		gbc_textPane.gridy = 5;
		add(textPane, gbc_textPane);
		actualizar();

	}
	
	
	
	
	
	
	public void actualizar(){
		TransferDirector datos=new TransferDirector();
		SingletonControlador.getInstancia().evento(Evento.DIRECTOR_GET_LISTA_ESTRATEGIAS, datos);
		//Remover datos anteriores.
		while(((DefaultTableModel)table.getModel()).getRowCount()!=0){
			((DefaultTableModel)table.getModel()).removeRow(0);
		}
		
		listaEstrategias=new ArrayList<DirectorEstrategia>();
		
		for(Iterator<DirectorEstrategia> it=datos.getListaEstrategias().iterator();it.hasNext();){
			DirectorEstrategia dp=it.next();
			String estado= dp.getFechaResolucion()==null?"Pendiente":dp.isAprobado()?"Aprobado":"Denegado";
			if (!chckbxMostrarSoloPendientes.isSelected()||(dp.getFechaResolucion()==null && chckbxMostrarSoloPendientes.isSelected())){
		
				((DefaultTableModel)table.getModel()).addRow(new Object[]{dp.getMotivo(),dp.getFecha(),estado});
				listaEstrategias.add(dp);
			}
		}
		
	}

}
